﻿using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_nested_parent_is_null
    {
        private readonly Mapping<DomainPerson, GuiPerson> simplifiedMapping;
        private readonly DomainPerson domainPerson;
        private readonly GuiPerson guiPerson;

        public class DomainPerson { public DomainAddress Address { get; set; } }
        public class DomainAddress {public int Number { get; set; }}

        public class GuiPerson { public GuiAddress Address { get; set; } }
        public class GuiAddress { public int Number { get; set; } }

        public When_nested_parent_is_null()
        {
            simplifiedMapping = new Mapping<DomainPerson, GuiPerson>();
            simplifiedMapping.Relate(domain => domain.Address.Number, gui => gui.Address.Number);
            domainPerson = new DomainPerson { Address = new DomainAddress { Number = 0 } };
            guiPerson = new GuiPerson { Address = new GuiAddress { Number = 0 } };
        }

        [Fact]
        public void Should_not_map_at_all_towardsRight_on_child_when_using_simplified_mapping()
        {
            domainPerson.Address = null;
            guiPerson.Address.Number = 1;

            simplifiedMapping.Map(domainPerson, guiPerson);

            Assert.Equal(1,guiPerson.Address.Number);
        }

        [Fact]
        public void Should_not_map_at_all_towardsLeft_on_child_when_using_simplified_mapping()
        {
            guiPerson.Address = null;
            domainPerson.Address.Number = 1;

            simplifiedMapping.Map(guiPerson, domainPerson);

            Assert.Equal(1, domainPerson.Address.Number);
        }

        [Fact]
        public void Should_handle_it_when_using_simplified_mapping_even_if_parent_toObject_is_null()
        {
            domainPerson.Address = null;
            guiPerson.Address = null;

            simplifiedMapping.Map(domainPerson, guiPerson);

            Assert.Null(guiPerson.Address);
        }

        [Fact]
        public void Should_not_map_at_all_on_child_when_using_explicit_mapping()
        {
            var addressMapping = new Mapping<DomainAddress, GuiAddress>();
            addressMapping.Relate(domain => domain.Number, gui => gui.Number);
            var mapping = new Mapping<DomainPerson, GuiPerson>();
            mapping.Relate(domain=>domain.Address,gui=>gui.Address,addressMapping);

            domainPerson.Address = null;
            guiPerson.Address.Number =1;

            mapping.Map(domainPerson, guiPerson);

            Assert.Equal(1,guiPerson.Address.Number);
        }
    }
}
